package com.authine.cloudpivot.ext.util;

import com.authine.cloudpivot.metadata.design.dto.clientobject.domainmodel.schema.BizSchema;
import com.authine.cloudpivot.metadata.design.dto.clientobject.domainmodel.schema.Properties;
import com.authine.cloudpivot.metadata.design.dto.clientobject.domainmodel.schema.extensions.Extensions;
import com.authine.cloudpivot.metadata.design.dto.clientobject.domainmodel.schema.extensions.ValueRange;
import com.authine.mvp.app.launcher.domain.utills.ApplicationContextUtils;
import com.authine.mvp.app.launcher.utils.MetadataCacheHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class PropertiesUtils {

    private static MetadataCacheHelper metadataCacheHelper = ApplicationContextUtils.getBean(MetadataCacheHelper.class);

    public static List<Properties> getSchemaProperties(String appCode, String schemaCode) {
        BizSchema bizSchema = metadataCacheHelper.getBizSchema(appCode, schemaCode);
        if (Objects.isNull(bizSchema) || CollectionUtils.isEmpty(bizSchema.getProperties())) {
            return Collections.EMPTY_LIST;
        }
        return bizSchema.getProperties();
    }

    public static Properties getPropertyByCode(List<Properties> properties, String code) {
        Optional<Properties> first = properties.stream().filter(e -> code.equals(e.getCode())).findFirst();
        Assert.isTrue(first.isPresent(),"字段属性不存在");
        return first.get();
    }

    /**
     * 根据文本值获取key【单选控件】
     * @param properties
     * @param code
     * @param value
     * @return
     */
    public static String getKeyByValue(List<Properties> properties, String code, String value) {
        Properties properties1 = getPropertyByCode(properties,code);
        Extensions extensions = properties1.getExtensions();
        if (Objects.isNull(extensions)) {
            return null;
        }
        List<ValueRange> valueRange = extensions.getValueRange();
        if (CollectionUtils.isEmpty(valueRange)) {
            return null;
        }
        Optional<ValueRange> first = valueRange.stream().filter(e -> value.equals(e.getText())).findFirst();
        return first.map(ValueRange::getKey).orElse(null);
    }

    /**
     *
     * @param properties
     * @param code
     * @param key
     * @return
     */
    public static String getValueByKey(List<Properties> properties, String code, String key) {
        Properties properties2 = getPropertyByCode(properties,code);
        Extensions extensions = properties2.getExtensions();
        if (Objects.isNull(extensions)) {
            return StringUtils.EMPTY;
        }
        List<ValueRange> valueRange = extensions.getValueRange();
        if (CollectionUtils.isEmpty(valueRange)) {
            return StringUtils.EMPTY;
        }
        System.out.println(code + "获取匹配的ValueRange" + valueRange.toString());
        Optional<ValueRange> first = valueRange.stream().filter(e -> key.equals(e.getKey())).findFirst();
        return first.map(ValueRange::getText).orElse(StringUtils.EMPTY);
    }
}
